'use strict';

angular.module('myApp.login', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/login', {
    templateUrl: 'views/login/login.html',
    controller: 'LoginCtrl'
  });
}])

.controller('LoginCtrl', function($scope, $http, $rootScope) {
$scope.username = '';
$scope.password = '';
$scope.login = function(username, password) {


  var body = {
    'session': {
      'email': username,
      'password': password
    }
  };

  console.log('Attempting to signin with' + JSON.stringify(body));

  $http.post('http://localhost:3000/api/v1/signin',body)
  .success(function(data, status, headers, config) {
    console.log(status + ' ' + JSON.stringify(data));
    $scope.message = data.message;
    $rootScope.userMail = username;
    $rootScope.token = data.token;
  })
  .error(function(data, status, headers, config) {
    console.log(status + ' ' + JSON.stringify(data));
    $scope.message = data.message;
  });
};



});